
$(function(){
    $("#sendcode").click(function(){
    	sendcode();
    	return false;
    })
    $("#reg").click(function(){
    	reg();
    	return false;
    })
})
function sendcode(){
	var email= $("#email").val(); 
    var username= $("#username").val();
    var qq= $("#qq").val();
    var code_input = $("#code_input").val();
    var agree = document.getElementById('agree').checked;
    if(agree === false){
         falsemsg(layer,"请先勾选服务条款",function(){
               refreshCaptcha();//刷新验证码
          }); 
          return false;
    }
	var ii = layer.load(5, {shade:[0.1,'#fff']});
	$.ajax({
	    type : "POST",
	    url : "./ajax/send_reg_code",
	    data : {
	        user: username,
            qq: qq,
            email:email,
            captcha:code_input
	    },
	    dataType : 'json',
	    success : function(data){
	        layer.close(ii);
	        if(data.code == 200){
	            truemsg(layer,data.msg);
	        }else{
	           falsemsg(layer,data.msg,function(){
	               refreshCaptcha();//刷新验证码
	           }); 
	        }
	    },
		error:function(data){
    		layer.close(ii);
    		falsemsg(layer,"服务器错误",function(){
	       //   refreshCaptcha();//刷新验证码
	       }); 
		}
	});
}

function reg(){
    var user=$("#username").val();
	var pass=$("#password").val();
	var qq=$("#qq").val();
	var email=$("#email").val();
	var code=$("#code").val();
	var code_input = $("#code_input").val();
	var ii = layer.load(5, {shade:[0.1,'#fff']});
	$.ajax({
	    type : "POST",
	    url : "./ajax/reg",
	    data : {
	        user,
	        pass,
	        qq,
	        email,
	        code,
	        img_code :code_input
	    },
	    dataType : 'json',
		timeout: 15000, //ajax请求超时时间15s
		success : function(res){
		    layer.close(ii);
		    if(res.code == 200){
		        layer.msg(res.msg, {
						icon: 1,
						time: 1000
				},function(){
				    window.location.href="./login";
				});
		    }else if(res.code == 1){
		        layer.open({
                        type: 1,
                        title: '<i class="layui-icon layui-icon-rmb"></i> 选择支付方式',
                        area: ['400px', '300px'],
                        shadeClose: true,
                        skin: 'custom-layer-skin', // 使用自定义的 Skin
                        content: generateContent(res),
                        btn: false,
                        success: function(layero, index) {
                          $(layero).find('.pay-button').on('click', function() { // 修改选择器
                            var payType = $(this).data('type');
                            handlePay(payType, res.token, index);
                          });
                        }
                      });
                    
                      function generateContent(data) {
                        let content = `<div class="payment-container">
                                          <p class="payment-amount">支付金额：<span class="amount">${data.money}</span></p>
                                          <div class="payment-options">`;
                    
                        for (let key in data.pay_type) {
                          content += `<button class="pay-button" data-type="${key}">
                                        <img src="/assets/icon/${key}.ico" alt="${data.pay_type[key]}">
                                        ${data.pay_type[key]}
                                      </button>`;
                        }
                    
                        content += `</div></div>`;
                        return content;
                      }
                    
                      function handlePay(type, token, index) {
                        $.ajax({
                          type: "POST",
                          url: "./ajax/reg_pay",
                          data: {
                            type: type,
                            token: token,
                          },
                          dataType: 'json',
                          success: function(res) {
                            if (res.code == 200) {
                              layer.close(index);
                              layer.msg(res.msg, {
                                icon: 1,
                                time: 1000
                              }, function() {
                                window.location.href = res.pay_url;
                              });
                            } else {
                              layer.msg(res.msg || '拉起支付失败', {
                                icon: 2
                              });
                            }
                          },
                          error: function() {
                            falsemsg(layer, "服务器错误！");
                          }
                        });
                      }
                }else{
    		        falsemsg(layer,res.msg); 
    		        refreshCaptcha();
    		    }
		},
		error:function(res){
    		layer.close(ii);
    	    falsemsg(layer,"服务器错误"); 
		}
	});
}
 // 支付函数
function payNow(token,type) {
    
    $.ajax({
        type : "POST",
        url : "./ajax/reg_pay",
        data:{
            type:type,
            token:token
        },
        dataType : 'json',
        success : function(res){
            if(res.code == 200){
                layer.msg(res.msg, {
                    icon: 1,
                    time: 1000
                }, function() {
                    window.location.href = res.pay_url;
                });
            }else{
                layer.msg(res.msg || '拉起支付失败', {icon: 2});
            }
        },
        error:function(){
            falsemsg(layer , "服务器错误！");
        }
    });

}